/*
 * @copyright IMTT. Co. Ltd.
 * 
 * Here is detailed comment.
 */
package com.apis.web.goods;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.apis.bussiness.entity.impl.GoodsImpl;
import com.apis.bussiness.entries.impl.GoodsMgrImpl;
import com.apis.util.commons.XmlUtil;
import com.apis.web.Action;
import com.apis.web.WebConstants;
import com.apis.web.XmlResponse;


/**
 * @author LiHongChun
 * @date 2012-10-25
 * @copyright IMTT. Co. Ltd.
 */
public class RemoveGoods extends Action {

	/* (non-Javadoc)
	 * @see com.imtt.insect.web.Action#execute(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) {
		XmlResponse xml = new XmlResponse();
		xml.setRequest(Request(req));
		String data = req.getParameter(WebConstants.TAG_ACTION_DATA);
		if (data == null) {
			String reason = "操作失败: 缺少必要信息导致当前操作失败...";
			xml.setReason(reason);
		} else {
			Document document = null;
			try {
				document = XmlUtil.loadDocument(data);
			} catch (Exception e) {
				String reason = "操作失败: 未能将输入数据转换为有效的XML Document...";
				xml.setReason(reason);
				xml.setException(e);
			}

			GoodsMgrImpl manager = new GoodsMgrImpl();
			try {
				Element root = document.getDocumentElement();
				NodeList list = root.getElementsByTagName(WebConstants.TAG_GOODS);
				for (int i = 0; i < list.getLength(); i++) {
					Element e = (Element) list.item(i);
					Integer id = Integer.parseInt( e.getAttribute(WebConstants.TAG_GOODS_ID));
					GoodsImpl goods = (GoodsImpl) manager.get(GoodsImpl.class, id);
					manager.delete(goods);
					//manager.deleteById(GoodsImpl.class, 35); deleteById is not work.
					xml.setState(1);
				}
			} catch (NumberFormatException ne) {
				String reason = "操作失败: 商品ID错误...";
				xml.setReason(reason);
				xml.setException(ne);
			}
		}

		Response(resp, xml.toString());		
	}

	/* (non-Javadoc)
	 * @see com.imtt.insect.web.Action#getActionID()
	 */
	@Override
	public int getActionID() {
		// TODO Auto-generated method stub
		return 1002;
	}

}
